using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ItemItem : MonoBehaviour
{
    public Text m_NameT;
    public Text m_ValueT;
    public Image m_PreviewI;
    public Item m_Item;

    public Image m_PartBlockPrefab;
    public Image m_PartBlock;
    private void Awake()
    {
    }
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {

    }
    public virtual void Init(Item item)
    {
        m_Item = item;
        var itemItem = item as Item;
        m_NameT.text = itemItem.name;
        string path = Utils.GetDataPath() + itemItem.iconPath;
        m_PreviewI.sprite = Utils.LoadSpriteByIo(path, new Vector2(0.5f, 0.5f));

        

    }
    public void OnDragBegine(BaseEventData eventData)
    {
        var theNewItemContent = ItemManager.Instance.createItem(m_Item);
        Part part = theNewItemContent as Part;
        var partRadius = part.GetRadius();
        string path = Utils.GetDataPath() + part.iconPath;
        var partBlock = Instantiate(m_PartBlockPrefab);
        var shapeSize = part.GetShapeInfo();
        partBlock.sprite = Utils.LoadSpriteByIo(path, new Vector2(0.5f, 0.5f));
        partBlock.transform.SetParent(Utils.GetCanvas().transform);
        partBlock.rectTransform.sizeDelta = new Vector2(shapeSize.size.x * 50f, shapeSize.size.y * 50f);
        partBlock.rectTransform.pivot = new Vector2(shapeSize.pivot.x, shapeSize.pivot.y);
        partBlock.alphaHitTestMinimumThreshold = 0.1f;
        partBlock.transform.SetSiblingIndex(transform.parent.childCount + 1);
        partBlock.GetComponent<PartBlock>().m_Part = part;
        partBlock.GetComponent<PartBlock>().OnDragBegine(eventData);
        eventData.selectedObject = partBlock.gameObject;
        m_PartBlock = partBlock;

    }
    public void OnDrag(BaseEventData eventData)
    {
        if(m_PartBlock)
            m_PartBlock.transform.position = Input.mousePosition;
    }
    public void OnDragEnd(BaseEventData eventData)
    {
        m_PartBlock.GetComponent<PartBlock>().OnDragEnd(eventData);
    }
}
